package com.gu.controller;

import com.github.pagehelper.PageInfo;
import com.gu.VO.LayuiVO;
import com.gu.VO.ServerResponse;
import com.gu.entity.LeavingMessageDO;
import com.gu.entity.NoticeDO;
import com.gu.resultMap.LeavingMessageWithAreaCode;
import com.gu.service.LeavingMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ProjectName: apartment-management-center
 * @Package: com.gu.controller
 * @ClassName: LeavingMessageController
 * @Author: gusongliao
 * @Description: 留言控制器
 * @createDate 2021/04/04 16:11
 */
@Controller
@RequestMapping("leavingMsg")
public class LeavingMessageController {

    @Autowired
    private LeavingMessageService leavingMessageService;

    @GetMapping("/leavingMsgList")
    @ResponseBody
    public LayuiVO listLeavingMsg(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, String searchParam, String searchDate) {
        PageInfo notice = leavingMessageService.getLeavingMessageBySearchParam(pageNum, pageSize, searchParam, searchDate);
        return LayuiVO.success(notice.getTotal(),notice.getList());
    }


    @GetMapping("/leavingMessageList")
    public String listLeavingMessage(String searchParam, String searchDate, HttpServletRequest request) {
        List<LeavingMessageWithAreaCode> leavingMessageWithAreaCodes = leavingMessageService.getRecord(searchParam, searchDate);
        request.setAttribute("listMsg",leavingMessageWithAreaCodes);
        return "leavingMsgUser";
    }

    @PostMapping("/insertLeavingMsg")
    @ResponseBody
    public ServerResponse insertLeavingMsg(@RequestBody LeavingMessageDO leavingMessageDO) {
        return leavingMessageService.insertLeavingMessage(leavingMessageDO);
    }

    @RequestMapping("/viewLeavingMsg")
    public String viewLeavingMsg(Integer id, HttpServletRequest request) {
        System.out.println("********************************");
        System.out.println(id);
        System.out.println("********************************");
        LeavingMessageDO leavingMessageDO = leavingMessageService.getById(id);
        request.setAttribute("leavingMessage",leavingMessageDO.getReviews());
        return "viewNotices";
    }

    @GetMapping("/deleteLeavingMsg")
    @ResponseBody
    public ServerResponse deleteNotice(Integer[] id) {
        return leavingMessageService.deleteByIds(id);
    }

    @RequestMapping("/leavingMsg")
    public String notice(){
        return "leavingMessage";
    }

    @GetMapping("/getRecordByAreaCode")
    @ResponseBody
    public LayuiVO getRecordByAreaCode(String areaCode) {
        return LayuiVO.success(100000L,leavingMessageService.getRecordByAreaCode(areaCode).getData());
    }

    @GetMapping("/getRecordByAreaCodeNew")
    public String getRecordByAreaCodeNew(String areaCode, HttpServletRequest request) {
        List<LeavingMessageWithAreaCode> leavingMessageWithAreaCodes = (List<LeavingMessageWithAreaCode>) leavingMessageService.getRecordByAreaCode(areaCode).getData();
        request.setAttribute("leavingMessageWithAreaCodes",leavingMessageWithAreaCodes);
        return "leavingMsgUserDetail";
    }

    @PostMapping("/deleteLeavingMsg")
    @ResponseBody
    public ServerResponse deleteLeavingMsg(@RequestBody LeavingMessageDO leavingMessageDO) {
        return leavingMessageService.deleteLeavingMsg(leavingMessageDO);
    }
}
